package com.itboye.ihomebank.activity.myzhujia.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * 添加银行卡
 * 
 */
public class ActivityAddBankCard extends BaseOtherActivity implements Observer {
	View v_statusbar;
	TextView add_shap_title_tv;
	ImageView close_icon;

	EditText add_bank_num,add_bank_khh;
	TextView add_bank_name,add_idnumber;
	TextView add_bank_xuanze;
	TextView add_bank_ok;
	UserPresenter userPresenter;
	public static ActivityAddBankCard context;

	LinearLayout xinyongka;

	EditText add_bank_avv,add_bank_yxq;

	ArrayList<String> arrayList;

	public static ActivityAddBankCard getInstance() {
		return context;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.add_bank_xuanze:// 选择开户行

			break;
		case R.id.add_bank_ok:// 下一步
			String name = add_bank_name.getText().toString();
			String idNumber = add_idnumber.getText().toString();
			String cardNo = add_bank_num.getText().toString();
			String khh=add_bank_khh.getText().toString();
			if (cardNo.equals("")) {
				ByAlert.alert("请输入卡号");
				return;
			}
//			if (khh.equals("")) {
//				ByAlert.alert("请输入开户行");
//				return;
//			}
			String kaihuhang = add_bank_xuanze.getText().toString();
			if (kaihuhang.equals("")) {
				ByAlert.alert("银行卡有误");
				return;
			}
			Intent intent = new Intent(this, ActivityAddBankCardTwo.class);
			intent.putExtra("bank_account", name);
			intent.putExtra("bank_no", cardNo);
			intent.putExtra("idNumber", idNumber);
			intent.putExtra("khh", "");
			if(!arrayList.equals("")&&arrayList.get(2).equals("贷记卡")){
				String avv=add_bank_avv.getText().toString();
				String yxq=add_bank_yxq.getText().toString();
				if (avv.equals("")){
					ByAlert.alert("请输入后三位");
					return;
				}
				if (yxq.equals("")){
					ByAlert.alert("请输入有效期");
					return;
				}
				intent.putExtra("avv", avv);
				intent.putExtra("yxq", yxq);
			}else{
				intent.putExtra("avv", "");
				intent.putExtra("yxq", "");
			}

			startActivity(intent);
			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_add_bank_card;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		context = this;
		userPresenter = new UserPresenter(this);
		add_shap_title_tv.setText("添加银行卡");

		String name = SPUtils.get(this, null, SPContants.RealName, "") + "";
		String idCode = SPUtils.get(this, null, SPContants.IdNumBer, "") + "";
		add_bank_name.setText(name);
		add_idnumber.setText(idCode);

		add_bank_num.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				if (s.length() == 16) {
					userPresenter.getBankType(s.toString());
				}
				if(s.length()<16){
					add_bank_xuanze.setText("");
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.bankType_success) {
				arrayList = (ArrayList<String>) entity
						.getData();
				add_bank_xuanze.setText(arrayList.get(0));

				if(arrayList.get(2).equals("贷记卡")){
					xinyongka.setVisibility(View.VISIBLE);
				}else{
					xinyongka.setVisibility(View.GONE);
				}

			} else if (entity.getEventType() == UserPresenter.bankType_fail) {
					ByAlert.alert(entity.getData());
			}
		}
	}

}
